package com.example.football;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.football.util.JDBCUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class UppasswordActivity extends AppCompatActivity {

    static public Integer peopleid;
    static public String account;
    static public String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uppassword);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        peopleid = Main2Activity.peopleid;
        account = Main2Activity.account;
        name = Main2Activity.name;
    }

    public void uppassword_iv_back(View view){
        finish();
    }

    public void update(View view){
        final EditText et_jiupassword = findViewById(R.id.xiupassword);
        final EditText et_xinpassword = findViewById(R.id.xinpassword);
        final EditText et_xinpassword1 = findViewById(R.id.xinpassword1);
        String jiupassword = et_jiupassword.getText().toString().trim();
        final String xinpassword = et_xinpassword.getText().toString().trim();
        String xinpassword1 = et_xinpassword1.getText().toString().trim();
        if(jiupassword.equals("")||xinpassword.equals("")||xinpassword1.equals("")){
            Toast.makeText(UppasswordActivity.this, "密码不能为空!", Toast.LENGTH_SHORT).show();
        }else if(!xinpassword.equals(xinpassword1)){
            Toast.makeText(UppasswordActivity.this, "两次新密码不一致，请重新输入!", Toast.LENGTH_SHORT).show();
        }else {
            JSONObject jsonObject=new JSONObject();
            try {
                jsonObject.put("account",account);
                jsonObject.put("password",jiupassword);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String url="http://"+ JDBCUtils.ip +":9000/APPlogin";
            RequestQueue requestQueue= Volley.newRequestQueue(UppasswordActivity.this);
            JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    try {
                        Log.d("信息", jsonObject.toString());
                        String msg = jsonObject.getString("msg");
                        Log.d("msg", msg);
                        if(msg.equals("登录成功")){
                            updatePeople(peopleid,xinpassword);
                        }else if(msg.equals("用户名或密码错误")){
                            Toast.makeText(UppasswordActivity.this, "旧密码输入有误，请重新输入!", Toast.LENGTH_SHORT).show();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Toast.makeText(UppasswordActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
                }
            });
            requestQueue.add(jsonObjectRequest);
        }
    }

    public void updatePeople(int id,String password){
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("id",id);
            jsonObject.put("password",password);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String url="http://"+ JDBCUtils.ip +":9000/APPupdatepeople";
        RequestQueue requestQueue= Volley.newRequestQueue(UppasswordActivity.this);
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Toast.makeText(UppasswordActivity.this, "修改成功,请重新登录！", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(UppasswordActivity.this,MainActivity.class);
                intent.putExtra("account",account);
                startActivity(intent);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(UppasswordActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }
}
